package net.sinmetal.twitcockpit.controller.twitter;

import net.sinmetal.twitcockpit.twitter.Twitter4tc;
import net.sinmetal.twitcockpit.twitter.Twitter4tcFactory;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import twitter4j.auth.RequestToken;

/**
 * Twitter OAuth
 *
 * @author Sinmetal
 *
 */
public class IndexController extends Controller {

    /**
     * Twitter認証を行う
     */
    @Override
    public Navigation run() throws Exception {
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL
            .replace(index, callbackURL.length(), "")
            .append("/callback");

        Twitter4tc twitter = new Twitter4tcFactory().getInstance();
        RequestToken requestToken =
            twitter.getOAuthRequestToken(callbackURL.toString());

        sessionScope("requestToken", requestToken);
        response.sendRedirect(requestToken.getAuthorizationURL());
        return null;
    }
}
